<?php
namespace app\dsadmin\controller;
use app\dsadmin\model\GongyouModel;
use think\Db;
class Gongyou extends Base
{
    public function index()
    {
        header('Access-Control-Allow-Origin: *');
        if (input('post.alldel'))
        {
            Db::name('gongyou')->where('id', 'neq', '0')->delete();
            return json(['code' => '1', 'msg' => 'ok']);
        }

        $key = input('key');
        $map = [];
        if ($key && $key !== "")
        {
            $map['name'] = ['like', "%" . $key . "%"];
        }
        $typess = input('type');
        if ($typess && $typess !== "")
        {
            $map['type'] = $typess;
        }
        $map['del'] = 1;
        $map['pianku'] = 1;
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = 10; // 获取总条数
        $count = Db::name('gongyou')->where($map)->count(); //计算总页面
        $allpage = intval(ceil($count / $limits));
        $gongyou = new GongyouModel();
        $lists = $gongyou->getGongyouByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        if (input('get.page'))
        {
            foreach ($lists as &$v){
                // $lenth = mb_strlen("http://www.7mv3wx1.cn/video/m3u8/2020/07/09/70de5fb8/vod.jpg");
                // $photo = substr($v['photo'],0,52);
                // $v['photo1'] = $photo.'2_vod.jpg';
                // $v['photo2'] = $photo.'3_vod.jpg';
                // $v['photo3'] = $photo.'4_vod.jpg';
                // $v['photo4'] = $photo.'5_vod.jpg';
               
            }
            unset($v);
            return json($lists);
        }
        $type = db('type')->order('id desc')->select();
        $this->assign('type', $type);
        $this->assign('typess', $typess);
        return $this->fetch();
    }

    public function add()
    {
        if (request()->isAjax())
        {
            $param = input('post.');
            $gongyou = new GongyouModel();
            $flag = $gongyou->insertGongyou($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $type = db('type')->order('id desc')->select();
        $this->assign('type', $type);
        return $this->fetch();
    }

    public function shangchuan()
    {
        $newnamepath = "uploads/shipin/" . date("Ymd", time()) . "/" . md5(time() . mt_rand(1, 1000000));
        $this->assign('newnamepath', $newnamepath);
        $type = db('type')->order('id desc')->select();
        $this->assign('type', $type);
        return $this->fetch();
    }

    public function edit()
    {
        $gongyou = new GongyouModel();

        if (request()->isAjax())
        {
            $param = input('post.');
            $flag = $gongyou->updateGongyou($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');

        $this->assign('gongyou', $gongyou->getOneGongyou($id));
        $type = db('type')->order('id desc')->select();
        $this->assign('type', $type);
        return $this->fetch();
    }

    public function del()
    {
        $id = input('param.id');
        // $cate = new GongyouModel();
        // $flag = $cate->delGongyou($id);
        Db::name('gongyou')->where(['id'=>$id])->update(['del' => 1]);
        Db::name('siyou')->where(['pid'=>$id])->update(['del' => 1]);
        
        // return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }
    
    public function ding(){
      
        $id = input('id');
        $let = Db::name('siyou')->where(['pid' => $id])->update(['ding_time' => time()]);
        if($let){
            return json(['id' => $id, 'state' => 66, 'msg' => '置顶成功', 'zt' => 1]);
            
        }
       
        
    }
    
      public function tupian(){
      
        $id    = input('id');
        $urlh  = input('urlh');
        $let = Db::name('gongyou')->where(['id' => $id])->update(['photo' => $urlh,'del' => 1]);
       
    }
    
}
